package com.aqie.meet.ui.my;

import android.content.Intent;
import android.util.Log;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.aqie.libbase.event.EventManager;
import com.aqie.libbase.event.MessageEvent;
import com.aqie.libcommon.app.BaseActivity;
import com.aqie.libcommon.app.BaseApplication;
import com.aqie.libfactory.data.helper.DbHelper;
import com.aqie.libfactory.data.helper.SessionHelper;
import com.aqie.libfactory.model.api.RspModel;
import com.aqie.libfactory.model.db.Session;
import com.aqie.libfactory.model.db.User;
import com.aqie.libfactory.net.Network;
import com.aqie.libfactory.net.RemoteService;
import com.aqie.libfactory.persistence.Account;
import com.aqie.meet.R;
import com.aqie.meet.ui.login.AccountActivity;

import net.qiujuer.genius.ui.widget.Button;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MySettingActivity extends BaseActivity {

    @BindView(R.id.action_back)
    ImageView mImg;

    @BindView(R.id.logout)
    Button mLogout;

    @BindView(R.id.group)
    RadioGroup mGroup;

    @BindView(R.id.button1)
    RadioButton mButton1;

    @BindView(R.id.button2)
    RadioButton mButton2;

    private Integer boolChat;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_my_setting;
    }

    @OnClick(R.id.action_back)
    void back(){
        finish();
    }

    @OnClick(R.id.logout)
    void logout(){
        Account.logOut(this);
        List<Session> sessions = SessionHelper.searchAllSession();
        DbHelper.delete(Session.class,sessions.toArray(new Session[0]));
        BaseApplication.getInstance().finishAll();
        startActivity(new Intent(this, AccountActivity.class));
    }

    @Override
    protected void initData() {
        super.initData();
        Intent intent = getIntent();
        // 状态选中
        boolChat = intent.getIntExtra("boolChat", 1);
        if(boolChat == 1) {
            mButton1.setChecked(true);
        } else {
            mButton2.setChecked(true);
        }
        // Log.e(TAG, this.boolChat + " 当前选中状态");

    }

    @Override
    protected void initWidget() {
        super.initWidget();
        mGroup.setOnCheckedChangeListener(new MyRadioButtonListener());
    }

    class MyRadioButtonListener implements RadioGroup.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            User userInfoModel = new User();
            MessageEvent<Integer> event = null;
            // 选中状态改变时被触发
            switch (checkedId) {
                case R.id.button1:
                    // Log.e(TAG, "button1");
                    userInfoModel.setBoolChat(1);
                    event = new MessageEvent<>(EventManager.EVENT_REFRE_ME_INFO);
                    event.setData(1);
                    EventManager.post(event);
                    modifyUserInfo(userInfoModel);
                    break;
                case R.id.button2:
                    // Log.e(TAG, "button2");
                    userInfoModel.setBoolChat(2);
                    event = new MessageEvent<>(EventManager.EVENT_REFRE_ME_INFO);
                    event.setData(2);
                    EventManager.post(event);
                    modifyUserInfo(userInfoModel);
                    break;
            }
        }
    }

    protected void modifyUserInfo(User model){
        RemoteService service = Network.remote();
        Call<RspModel<Boolean>> call = service.mineUpdate(model);
        call.enqueue(new Callback<RspModel<Boolean>>() {
            @Override
            public void onResponse(Call<RspModel<Boolean>> call, Response<RspModel<Boolean>> response) {

                RspModel<Boolean> body = response.body();
                if(response.code() == 401){
                    Log.e(TAG, "尚未登录");
                    AccountActivity.show(getApplication());
                    return;
                }
                if(body !=null && body.success()){
                    // BaseApplication.showToast("操作成功");
                }else {
                    Log.e(TAG, "request fail");
                }
            }

            @Override
            public void onFailure(Call<RspModel<Boolean>> call, Throwable t) {
                Log.e(TAG, t.getMessage());
            }
        });
    }
}